const BugModel = require('../model/bug')
const UserModel = require('../model/user')
module.exports = (router) => {
    // 管理员查看bug 支持带参数和不带参数两种请求方式
    router.get('/bugList', function (req, res) {
        let isDeal = req.query.isDeal
        BugModel.find(isDeal === undefined ? {} : {isDeal}, function (err, bugList) {
            if (!err) {
                res.send({ code: 0, data: bugList })
            } else {
                res.send({ code: 1, msg: err })
            }
        })
    })

    //更新bug状态为已解决
    router.get('/updateBugState', function (req, res) {
        let { bugID } = req.query
        BugModel.updateOne({ _id: bugID }, { isDeal: true }, function (err, doc) {
            if (!err) {
                res.send({ code: 0, msg: 'bug已经修复' })
            } else {
                res.send({ code: 1, msg: '后台出现bug' })
            }
        })

    })
    // 临时接口，上线作废 给bug信息中增加提出人姓名 前端通过点击按钮传递过来提出人ID 
    router.post('/addAuthorName', function(req, res){
        let {userID, bugID} = req.body
        UserModel.findOne({_id: userID},function(err,user){
            // console.log(user)
            if(!user) {
                res.send({msg:'没有这个人'})
                return 
            }
            BugModel.findOneAndUpdate({_id: bugID},{authorName: user.userName},function(err,bug){
                if(!err)res.send({code: 0, msg: '更新成功'})
                else res.send({code: 1, msg: '更新失败'})
            })
        })
        
    })
}